using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Web;
using DotNetNuke.Common.Utilities;

namespace Code5Systems.FormMaster.Business
{

	#region Class FormFileDataController

	public class FormFileDataController
	{
		
#region Private Members
		int _intReportRecCount = 0;
		string _sharedResourceFile;
#endregion
		
#region Public Contructors
		public FormFileDataController()
		{
			_sharedResourceFile = Utility.GetSharedResourceFile();
		}
#endregion
		
#region Public Methods
		public FormFileDataInfo Get(int fileDataId)
		{
			
			return ((FormFileDataInfo) (CBO.FillObject(Code5Systems.FormMaster.Data.DataProvider.Instance().GetFormFileData(fileDataId), typeof(FormFileDataInfo))));
			
		}
		
		public List<FormFileDataInfo> List()
		{
			
			return CBO.FillCollection<FormFileDataInfo>(Code5Systems.FormMaster.Data.DataProvider.Instance().ListFormFileData());
			
		}
		
		public List<FormFileDataInfo> GetByFormData(int formDataID)
		{
			
			return CBO.FillCollection<FormFileDataInfo>(Code5Systems.FormMaster.Data.DataProvider.Instance().GetFormFileDataByFormData(formDataID));
			
		}
		
		public FormFileDataInfo GetByControlId(int formDataId, string controlId)
		{
			
			return ((FormFileDataInfo) (DotNetNuke.Common.Utilities.CBO.FillObject(Code5Systems.FormMaster.Data.DataProvider.Instance().GetFormFileDataByControlId(formDataId, controlId), typeof(FormFileDataInfo))));
			
		}
		
		public int Add(FormFileDataInfo objFormFileData, SqlTransaction objTransaction = null)
		{
			
			return System.Convert.ToInt32(Code5Systems.FormMaster.Data.DataProvider.Instance().AddFormFileData(objFormFileData.FormDataId, objFormFileData.PortalId, objFormFileData.ModuleId, objFormFileData.ControlId, objFormFileData.ContentType, objFormFileData.ContentLegth, objFormFileData.FilePath, objFormFileData.OrigFileName, objFormFileData.FileName, objFormFileData.InDB, objFormFileData.FileData, objFormFileData.CreatedByID, objTransaction));
			
		}
		
		public void Update(FormFileDataInfo objFormFileData, SqlTransaction objTransaction = null)
		{
			
			Code5Systems.FormMaster.Data.DataProvider.Instance().UpdateFormFileData(objFormFileData.FileDataId, objFormFileData.FormDataId, objFormFileData.ContentType, objFormFileData.ContentLegth, objFormFileData.FilePath, objFormFileData.OrigFileName, objFormFileData.FileName, objFormFileData.InDB, objFormFileData.FileData, objFormFileData.LastModifiedByID, objTransaction);
			
		}
		
		public void Delete(int fileDataId, SqlTransaction objTransaction = null)
		{
			
			Code5Systems.FormMaster.Data.DataProvider.Instance().DeleteFormFileData(fileDataId, objTransaction);
			
		}
		
		public void DeleteFile(int fileDataId)
		{
			SqlTransaction trans = null;
			SqlConnection conn = new SqlConnection();
			try
			{
				// Start a Transaction in case any part of the update fails
				// We are updateing multiple tables and deleting disk files
				conn = new SqlConnection(Code5Systems.FormMaster.Data.DataProvider.Instance().ConnectionString);
				
				
				//Get the File Information
				FormFileDataInfo objFileInfo = this.Get(fileDataId);
				
				if (objFileInfo != null)
				{
					
					conn.Open();
					
					trans = conn.BeginTransaction();
					
					//Delete the File Upload Record
					Delete(fileDataId, trans);
					
					//No Problems to this point so delete the File System files
					//This is of course permanent no Rollback here
					//Worse case is Orphaned files
					if (File.Exists(HttpContext.Current.Server.MapPath(objFileInfo.FullFilePath)))
					{
						File.Delete(HttpContext.Current.Server.MapPath(objFileInfo.FullFilePath));
					}
				}
				
				//Since we are dealing with only one record, we commit after
				//deleting the file, if there is an error deleting the file
				//the Data delete will be rolled back with no orphaned file.
				
				//Process the Database updates
				trans.Commit();
				
			}
			catch (IOException ioexc)
			{
				trans.Rollback();
				throw (ioexc);
			}
			catch (SqlException dbexc)
			{
				trans.Rollback();
				throw (dbexc);
			}
			catch (Exception exc)
			{
				trans.Rollback();
				throw (exc);
			}
			finally
			{
				if (conn.State == ConnectionState.Open)
				{
					conn.Close();
				}
			}
			
		}
		
		public DataTable GetFilesByModuleID(int moduleId)
		{
			
			return DotNetNuke.Common.Globals.ConvertDataReaderToDataTable(Code5Systems.FormMaster.Data.DataProvider.Instance().GetFilesByModuleID(moduleId));
			
		}
#endregion
		
#region Public Properties
		
		public string SharedResouceFile
		{
			get
			{
				return _sharedResourceFile;
			}
		}
		
#endregion
		
	}
	
#endregion
	
}


